<?php include _include(ADMIN_PATH.'view/htm/header.inc.htm');?>

<div class="row m-t-1">
	<div class="col-lg-5 offset-lg-3">
		<div class="card">
			<div class="card-block">
				<h4 class="bd-title">管理员登录</h4>
				<hr />
				<form action="<?php echo url('index-login');?>" method="post" id="form">
					<p class="input-group">
						<span class="input-group-addon" id="basic-addon1"><i class="icon icon-user"></i></span>
						<input type="text" class="form-control" placeholder="用户名" id="username" name="username">
					</p>
					<p class="input-group" style="margin-top: 20px">
						<span class="input-group-addon" id="basic-addon1"><i class="icon icon-lock"></i></span>
						<input type="password" class="form-control" placeholder="密码" id="password" name="password">
					</p>
					<p class="input-group" style="margin-top: 20px">
						<span class="input-group-addon" id="basic-addon1"><i class="icon icon-lock"></i></span>
						<input style="width: 70%" type="text" class="form-control" placeholder="验证码" id="yzm" name="yzm">
						<button type="button" id="sendsms" data-phone="<?php echo $user['info_mobile']?>" style="width: 25%;margin-left: 5%;float: left;height: 38px;">获取验证码</button>
					</p>
					<p class="text-xs-right" style="margin-top: 20px">
						<button type="submit" class="btn btn-primary btn-block" id="submit" data-loading-text="<?php echo lang('submiting');?>...">登录</button>
					</p>
				</form>
			</div>
		</div>
	</div>
</div>

<?php include _include(ADMIN_PATH.'view/htm/footer.inc.htm');?>
<script src="view/js/md5.js"></script>

<script>
var jform = $('#form');
var jsubmit = $('#submit');
var jemail = $('#email');
var jpassword = $('#password');
var layer;
layui.use(['layer', 'form'], function(){
    layer = layui.layer
        ,form = layui.form;

});
jform.on('submit', function() {
	jform.reset();
	jsubmit.button('loading');
	var postdata = jform.serializeObject();
	postdata.password = $.md5(postdata.password);
	$.xpost(jform.attr('action'), postdata, function(code, message) {
		if(code == 0) {
			jsubmit.button(message).delay(1000).location('./');
		} else if(code == 'email') {
			jemail.alert(message).focus();
			jsubmit.button('reset');
		} else if(code == 'password') {
			jpassword.alert(message).focus();
			jsubmit.button('reset');
		} else {
			alert(message);
			jsubmit.button('reset');
		}
	});
	return false;
});


//发送验证码时添加cookie
    function addCookie(name,value,expiresHours){
//判断是否设置过期时间,0代表关闭浏览器时失效
        if(expiresHours>0){
            var date=new Date();
            date.setTime(date.getTime()+expiresHours*1000);
            $.cookie(name, escape(value), {expires: date});
        }else{
            $.cookie(name, escape(value));
        }
    }
//修改cookie的值
    function editCookie(name,value,expiresHours){
        if(expiresHours>0){
            var date=new Date();
            date.setTime(date.getTime()+expiresHours*1000); //单位是毫秒
            $.cookie(name, escape(value), {expires: date});
        } else{
            $.cookie(name, escape(value));
        }
    }
//根据名字获取cookie的值
    function getCookieValue(name){
        return $.cookie(name);
    }

    v = getCookieValue("secondsremained");//获取cookie值
    if(v>0){
        settime($("#sendsms"));//开始倒计时
    }
    var obj = $('#sendsms');
    $(function(){
        $('#sendsms').click(function () {
				var username = $('#username').val();
				if(!username){
                    layer.alert('请先填写用户名', {icon: 1});
                    return false;
				}
                $.post("<?php echo url('index-sendAdmin')?>",{username:username},function (data) {
                    layer.alert(data.data, {icon: 1});
                    if(data.code==1){

                        addCookie("secondsremained",90,90);//添加cookie记录,有效时间60s
                        settime(obj);//开始倒计时

                    }else {

                    }

                })
        })
    })

//开始倒计时
    var countdown;
    function settime(obj) {
        countdown=getCookieValue("secondsremained");
        console.log(countdown)
        if (countdown == undefined) {
            obj.removeAttr("disabled");
            obj.html("获取验证码");
            return;
        } else {
            obj.attr("disabled", true);
            obj.html("重新发送需要(" + countdown + ")");
            countdown--;
            editCookie("secondsremained",countdown,countdown+1);
        }
        setTimeout(function() { settime(obj) },1000) //每1000毫秒执行一次
    }
//校验手机号是否合法
    function isPhoneNum(phone){

        var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
        if(!myreg.test(phone)){
            layer.alert('手机号不合法或者没有手机号1', {icon: 1});
            return false;
        }else{
            return true;
        }
    }

</script>